<template>
  <div class="mod-role">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!-- <el-button type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除 -->
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
          :on-success="handleSuccess"
          :show-file-list="false"
          :beforeUpload="beforeAvatarUpload"
          style="display:inline-block;margin-left:10px;">
          <el-button slot="trigger" class="elBtn" type="warning">批量导入</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="index"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        min-width="15%"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="ipAdress"
        header-align="center"
        align="center"
        min-width="30%"
        label="IP地址">
      </el-table-column>
      <el-table-column
        prop="macAdress"
        header-align="center"
        align="center"
        min-width="30%"
        label="MAC地址">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="description"
        header-align="center"
        min-width="20%"
        align="center"
        label="描述">
      </el-table-column>
      <el-table-column
        prop="unique"
        header-align="center"
        align="center"
        width="180"
        label="唯一性">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isUnique === 1" size="small" type="success">唯一</el-tag>
          <el-tag v-else-if="scope.row.isUnique === 0" size="small" >不唯一</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <!-- <el-button type="text" size="small"
                     @click="addOrUpdateHandle(scope.row.id)">修改
          </el-button> -->
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[13, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './ipMac-add-or-update'
  import Vue from 'vue'
  export default {
    data () {
      return {
        loading: false,
        searchForm: {
          name: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 13,
        totalPage: 0,
        dataListSelections: [],
        addOrUpdateVisible: false,
        uploadUrl: window.SITE_CONFIG['baseUrl'] + '/ipmacbind/ip-mac-bind/file/mac' + '?token=' + Vue.cookie.get('token')
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.$http({
          url: '/ipmacbind/ip-mac-bind/list',
          method: 'get',
          params: {
            'page': this.pageIndex,
            'limit': this.pageSize,
            'word': this.searchForm.name
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.records
            this.totalPage = data.page.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        // let ids = id ? [id] : this.dataListSelections.map(item => {
        //   return item.roleId
        // })
        this.$confirm(`确定对[id=${id}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: `/ipmacbind/ip-mac-bind/delete?id=${id}`,
            method: 'delete'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: data.msg,
                type: 'success',
                duration: 1500
              })
              this.getDataList()
            }
          })
        }).catch(() => {
        })
      },
      handleProgress () {
        this.loading = true
      },
      handleSuccess (res) {
        this.loading = false
        this.$message({
          message: res.msg,
          type: res.code === 0 ? 'success' : 'warning',
          duration: 1500
        })
        if (res.code === 0) {
          this.getDataList()
        }
      },
      beforeAvatarUpload (file) {
        var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
        const extension = testmsg === 'xls'
        const extension2 = testmsg === 'xlsx'
        if (!extension && !extension2) {
          this.$message({
            message: '只能上传xls、xlsx格式的文件!',
            type: 'warning'
          })
        }
        return extension || extension2
      }
    }
  }
</script>
